package com.css.dzj.dzjfxyj.modules.acquier.Controller;

import com.alibaba.fastjson.JSON;
import com.css.dzj.dzjfxyj.common.audit.service.SysAuditService;
import com.css.dzj.dzjfxyj.common.util.Constant;
import com.css.dzj.dzjfxyj.common.util.R;
import com.css.dzj.dzjfxyj.modules.acquier.entity.JgFxyjWarningSignalEntity;
import com.css.dzj.dzjfxyj.modules.acquier.service.AcquierService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author
 * @description 风险预警认领
 * @date 2020/5/26 20:00
 */
@RestController
@RequestMapping(value = "/acquier")
public class AcquierController {
    @Autowired
    private AcquierService acquierService;
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 分页查询所有(认领)
     */
    @GetMapping(value = "/getAllAcquier")
    public R getAllAcquier(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize,
                           @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                           @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                           @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                           @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                           @RequestParam(name = "startTime1", required = false) String startTime1,
                           @RequestParam(name = "endTime1", required = false) String endTime1,
                           @RequestParam(name = "startTime", required = false) String startTime,
                           @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults results = acquierService.selectAll(fxyjDomainId, fxyjTitle, fxyjLevel, fxyjObjectName, startTime1, endTime1, startTime, endTime, pageRequest);
        //baseService.logSuccess(3, "查询成功", "2");
        String s = JSON.toJSONString(results);
        sysAuditService.saveSuccessAudit("分页查询所有(认领)成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
        return R.ok().put("data", results);
    }

    /**
     * 分页查询所有(综合)
     */
    @GetMapping(value = "/getAllAcquierzh")
    public R getAllAcquierzh(@RequestParam(required = false) Integer pageNo,
                             @RequestParam(required = false) Integer pageSize,
                             @RequestParam(name = "fxyjDomainId", required = false) String fxyjDomainId,
                             @RequestParam(name = "fxyjTitle", required = false) String fxyjTitle,
                             @RequestParam(name = "fxyjLevel", required = false) String fxyjLevel,
                             @RequestParam(name = "fxyjObjectName", required = false) String fxyjObjectName,
                             @RequestParam(name = "dealState", required = false) String dealState,
                             @RequestParam(name = "startTime", required = false) String startTime,
                             @RequestParam(name = "endTime", required = false) String endTime) {
        PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
        QueryResults results = acquierService.selectAllzh(fxyjDomainId, fxyjTitle, fxyjLevel, fxyjObjectName, dealState, startTime, endTime, pageRequest);
        //baseService.logSuccess(3, "查询成功", "2");
        String s = JSON.toJSONString(results);
        sysAuditService.saveSuccessAudit("分页查询所有(综合)成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_PAGE, s, request);
        return R.ok().put("data", results);
    }

    /**
     * 根据id查询详细信息
     *
     * @return
     */
    @PostMapping("/findById")
    public R findById(String id) {
        String s = "";
        if (StringUtils.isEmpty(id)) {
            //baseService.logFailed(3, "查询失败", "2");
            s = JSON.toJSONString(id);
            RuntimeException e = new RuntimeException("id为空");
            sysAuditService.saveFailAudit("根据id查询详细信息失败", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, e, request);
            return null;
        } else {
            JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity = acquierService.findById(id);
            //baseService.logSuccess(3, "查询失败", "2");
            s = JSON.toJSONString(jgFxyjWarningSignalEntity);
            sysAuditService.saveSuccessAudit("根据id查询详细信息成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
            return R.ok().put("data", jgFxyjWarningSignalEntity);
        }
    }

    /**
     * 保存
     *
     * @param jgFxyjWarningSignalEntity
     * @param request
     * @return
     */
    @PostMapping("/saveAcquire")
    public R saveAcquire(JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity, HttpServletRequest request) {
        String s = "";
        try {
            R r = acquierService.saveAcquire(jgFxyjWarningSignalEntity, request);
            if (r.getCode() == "500") {
                //baseService.logFailed(3, "保存失败", "2");
                s = JSON.toJSONString(r);
                RuntimeException e = new RuntimeException(r.getMsg());
                sysAuditService.saveFailAudit("根据id查询详细信息成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, e, request);
                return R.error(r.getMsg());
            } else {
                //baseService.logFailed(3, "保存成功", "2");
                s = JSON.toJSONString(null);
                sysAuditService.saveSuccessAudit("根据id查询详细信息成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, request);
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "保存失败", "2");
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("根据id查询详细信息成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 认领操作修改
     *
     * @param jgFxyjWarningSignalEntity
     * @param request
     * @return
     */
    @PostMapping("/updAcquire")
    public R updAcquire(@RequestBody JgFxyjWarningSignalEntity jgFxyjWarningSignalEntity, HttpServletRequest request) {
        String s = "";
        try {
            acquierService.updAcquire(jgFxyjWarningSignalEntity, request);
            //baseService.logSuccess(3, "认领操作修改成功", "2");
            s = JSON.toJSONString(jgFxyjWarningSignalEntity);
            sysAuditService.saveSuccessAudit("认领操作修改成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "认领操作修改失败", "2");
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("认领操作修改失败", Constant.AUDIT_MODULE_ACQUIER, Constant.YZ,Constant.AUDIT_TYPE_UPDATE,s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改状态
     *
     * @param id
     * @return
     */
    @PostMapping("/updState")
    public R updState(String id) {
        String s = "";
        try {
            R r = acquierService.updState(id);
            if (r.getCode() == "500") {
                //baseService.logFailed(3, "修改失败", "2");
                s = JSON.toJSONString(r);
                RuntimeException e = new RuntimeException(r.getMsg());
                sysAuditService.saveFailAudit("修改状态失败", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, e, request);
                return R.error(r.getMsg());
            } else {
                //baseService.logSuccess(3, "修改成功", "2");
                s = JSON.toJSONString(r);
                sysAuditService.saveSuccessAudit("修改状态成功", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, request);
                return R.ok();
            }
        } catch (Exception e) {
            e.printStackTrace();
            //baseService.logFailed(3, "修改失败", "2");
            s = JSON.toJSONString(null);
            sysAuditService.saveFailAudit("修改状态失败", Constant.AUDIT_MODULE_ACQUIER, Constant.PT, Constant.AUDIT_TYPE_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * @description 获取数量
     * @author
     * @date 2020/5/27 9:50
     */
    @GetMapping("/account")
    public Long getCount() {
        Long count = acquierService.getCount();
        return count;
    }
}
